﻿using System.Collections.Generic;
using AdventureWorks.Domain;
using NHibernate;
using AdventureWorks.DataAccess.Utils;
using AdventureWorks.DataAccess.SearchCriterias;
using System.Linq;
using System;
using AdventureWorks.DataAccess.Specifications;
using AutoMapper;
using AdventureWorks.DataAccess.Repositories;

namespace AdventureWorks.DataAccess.Task
{
    public class ProductTask : IProductTask
    {
        

        /*public ProductTask(IProductRepository repository, ISessionFactory session)
        {
            this.repository = repository;
            this.sessionFactory = session;
        }

        public ProductTask() : this(new ProductRepository(), SessionManager.SessionFactory) { }

        public IEnumerable<Product> GetProducts(ProductSearchCriteria criteria)
        {
            using (ISession session = sessionFactory.OpenSession())
            {
               return repository.GetProducts(session, criteria).Paging(criteria).ToList();
            }            
        }*/
        public PagedResult<Product> GetSpecialOffers(ProductSearchCriteria searchCriteria)
        {
            MainMapper.MapProduct();
            var specification = Mapper.Map<ProductSearchCriteria, ProductSpecification>(searchCriteria);
            var d = SessionManager.SessionFactory.OpenSession();
            var repository = new NHibernateRepository<Product>(d);

            var c = repository.Find(specification);
            var x = c.Paging(searchCriteria).ToList();
            var g = new PagedResult<Product>
            {
                PageResult = c.Paging(searchCriteria).ToList(),
                Page = searchCriteria.Page,
                PageSize = searchCriteria.PageSize
            };
            return g;
        }

    }
}
